/*
 * $HeadURL:  $
 *
 * Copyright (c) 2007 Drutt Corporation, all rights reserved.
 *
 */
package homework.james.xiao.bean;

import homework.james.xiao.util.Util;

import java.io.IOException;
import java.io.ObjectOutputStream;

/**
 * @author eyngxao
 * @version $Revision: $
 */
public class Admin {

	/** Class revision */
	public static final String _REV_ID_ = "$Revision: $";
	private ObjectOutputStream oos;
	public static final int EXIT =1;
	public static final int SHUTDOWN = 0;
	public static final int OTHER = 2;
	public Admin(){
		
	}
	public Admin(ObjectOutputStream oos) {
		this.oos = oos;
	}

	public int handle(String command) throws IOException {
		String message = "";
		int result = OTHER;
		if (command.startsWith("shutdown")) {
			if (command.split(" ").length != 2) {
				message = "Wrong args for book command";
			} else {
				message = shutDown(command.split(" ")[1]);
				if(message.equals("shutdown"))
					result = SHUTDOWN;
			}
		} else if(command.equals("exit"))
				result =EXIT;
		else
			message = "invalid command";
		oos.writeObject(message);
		return result;
	}

	/**
	 * 关闭服务器
	 * 
	 * @param string
	 * @return
	 * @author James Xiao
	 * @datetime Sep 22, 20094:17:49 PM
	 */
	public String shutDown(String password) {
		String message;
		if (password.equals(Util.PASSWORD))
			message = "shutdown";
		else
			message = "wrong password";
		return message;
	}

	/**
	 * 
	 * @author James Xiao  
	 * @datetime Sep 23, 20095:02:22 PM
	 */
	public void printHelp() {
		System.out.println("Usage:");
		System.out.println("exit\t\t\t\t| exit the system");
		System.out.println("shutdown [password]\t\t| shutdown the system");
		
	}
}
